﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using Commonlib;

namespace AlbonWeb.Manager.StyleManager
{
    public partial class AdvancedSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定性别信息
                string cmdstr = "select * from Gender";
                Commonlib.SqlHelper.BindDropDownList(ddlGender, "genderName", "ID", Commonlib.SqlHelper.SqlConnectionString, System.Data.CommandType.Text, cmdstr, null);
                ddlGender.Items.Insert(0, new ListItem("-Gender-", "0"));
            }
        }

        //搜索
        protected void btnSearch_Click(object sender, EventArgs e)
        {
            //active
            string limit = " where active=" + (isActive.Checked ? "1" : "0");
            //styleName
            if (styleName.Text.Trim() != "")
            {
                limit += " and  styleName like '%"+styleName.Text.Trim()+"%'";
            }
            //price
            if (rbtnCharge.SelectedIndex != 0)
            {
                limit += " and ID "+(rbtnCharge.SelectedValue=="1"?"not":"")+" in (select styleID from StylePrice)";
            }
            //sex
            if (ddlGender.SelectedIndex != 0)
            {
                limit += " and sex=" + ddlGender.SelectedValue;
            }
            //select mode
            if (rbtnMode.SelectedIndex != 0)
            {
                limit += " and selectMode='"+rbtnMode.SelectedItem.Text+"'";
            }
            //buy cloth
            if (rbtnBuyCloth.SelectedIndex != 0)
            {
                limit += " and buyClothMark="+rbtnBuyCloth.SelectedValue;
            }
            //create Date
            if (tbFrom.Text.Trim() != "")
            {
                limit += " and createDate>'"+tbFrom.Text+"'";
            }
            if (tbTo.Text.Trim() != "")
            {
                limit += " and createDate<'"+tbTo.Text+"'";
            }
            ViewState["limit"] = limit;
            bindStyle();
        }
        //绑定搜索到的结果
        protected void bindStyle()
        { 
            string cmdstr = "select count(*) from StyleView" +ViewState["limit"].ToString();
            AspNetPager1.RecordCount = int.Parse(SqlHelper.ExecuteScalar(SqlHelper.SqlConnectionString,CommandType.Text,cmdstr,null).ToString());
            SqlParameter[] parameters = new SqlParameter[]{
                new SqlParameter("@tablename","StyleView"),
                new SqlParameter("@startIndex",AspNetPager1.StartRecordIndex),
                new  SqlParameter("@endIndex",AspNetPager1.EndRecordIndex),
                new SqlParameter("@limit",ViewState["limit"].ToString()),
                new SqlParameter("@orderby","createDate desc")
            };
            SqlHelper.BindRepeater(repMain, SqlHelper.SqlConnectionString, CommandType.StoredProcedure, "netpager", parameters);  
        }
        //换页
        protected void AspNetPager1_PageChanging(object src, Wuqi.Webdiyer.PageChangingEventArgs e)
        {
            AspNetPager1.CurrentPageIndex = e.NewPageIndex;
            bindStyle();
        }
        //绑定样式的父样式名称和顶级样式名称
        protected void repMain_ItemDataBound(object sender, RepeaterItemEventArgs e)
        {
            if (e.Item.ItemIndex != -1)
            {
                string id = ((HiddenField)e.Item.FindControl("hfstyleid")).Value;
                string parentID = ((HiddenField)e.Item.FindControl("hfparentid")).Value;
                Label lbParent = (Label)e.Item.FindControl("parentStyle");
                Label lbTop = (Label)e.Item.FindControl("topStyle");
                BLL.StyleBLL stylebll = new BLL.StyleBLL();
                if (parentID != "0")
                {
                    lbParent.Text = stylebll.getStyleName(parentID);
                    lbTop.Text = stylebll.getTopStyle(id);
                }
            }
        }
        //删除查询出来的样式
        protected void imgDelete_Click(object sender, ImageClickEventArgs e)
        {
            BLL.StyleBLL stylebll = new BLL.StyleBLL();
            ImageButton imgDelete = (ImageButton)sender;
            stylebll.DeleteStyle(imgDelete.CommandArgument);
            bindStyle();
        }
    }
}